package com.nuonuo.nuo_xp;

/**
 * @author glsite.com
 * @version $Rev$
 * @des ${TODO}
 * @updateAuthor $Author$
 * @updateDes ${TODO}
 */

import android.util.Base64;
import android.util.Log;
import java.security.MessageDigest;
import de.robv.android.xposed.IXposedHookLoadPackage;
import de.robv.android.xposed.XC_MethodHook;
import de.robv.android.xposed.XposedBridge;
import de.robv.android.xposed.XposedHelpers;
import de.robv.android.xposed.callbacks.XC_LoadPackage;

/**
 *
 * Hook通杀MD5和SHA
 * hook类
 * java.security.MessageDigest
 *
 */

public class HOOK_MD5 implements IXposedHookLoadPackage {
    private String log_d_tag="XP";
    @Override
    public void handleLoadPackage(XC_LoadPackage.LoadPackageParam loadPackageParam) throws Throwable {
        Log.d(log_d_tag,"注入成功...");
        Log.d(log_d_tag,"***信息散列算法***");
        Log.d(log_d_tag,"HOOK_class:java.security.MessageDigest");
        Log.d(log_d_tag,"活动APP包名:"+loadPackageParam.packageName);
        //jdk里面的MD5和sha加密包java.security.MessageDigest
        XposedBridge.hookAllMethods(
                XposedHelpers.findClass("java.security.MessageDigest",
                loadPackageParam.classLoader),
                "update",
                new XC_MethodHook() {
            @Override
            //updata函数没用返回值用beforeHookedMethod处理
            protected void beforeHookedMethod(MethodHookParam param) throws Throwable {
                MessageDigest messageDigest = (MessageDigest)param.thisObject;//获取类的字节码
                String algorithm = messageDigest.getAlgorithm();//获取加密类型,such as "SHA", "MD5"
                byte[] bytes = (byte[]) param.args[0];//获取第一个参数
                String data = new String(bytes);
                //String dataHex = Utils.byteToHexString(bytes);//明文hex形式
                String dataBase64 = Base64.encodeToString(bytes,0);
                Log.d(log_d_tag,"-");
                Log.d(log_d_tag,"-");
                Log.d(log_d_tag,"=====================================HOOK开始==============================================");
                Log.d(log_d_tag,"=====================================Class_MD5==============================================");
                Log.d(log_d_tag,algorithm+"_update:"+data);
                Log.d(log_d_tag,algorithm+"_update_Base64:"+dataBase64);
                Log.d(log_d_tag,"============================算法:"+algorithm+"方法:update==================================");
                Log.d(log_d_tag,"=====================================HOOK结束==============================================");
                Log.d(log_d_tag,"=====================================调用堆栈~开始==============================================");
                Log.d(log_d_tag,Log.getStackTraceString(new Throwable()));
                Log.d(log_d_tag,"=====================================调用堆栈~结束==============================================");
            }
        });


        XposedBridge.hookAllMethods(XposedHelpers.findClass("java.security.MessageDigest", loadPackageParam.classLoader),
                "digest", new XC_MethodHook() {
                    @Override
                    protected void afterHookedMethod(MethodHookParam param) throws Throwable {
                        MessageDigest messageDigest = (MessageDigest)param.thisObject;
                        String algorithm = messageDigest.getAlgorithm();//获取加密类型,such as "SHA", "MD5"
                        //digest方法可以取加密结果也可以压入加密数据
                        if (param.args!=null){
                            if (param.args.length>0){
                                byte[] bytes = (byte[]) param.args[0];
                                String data = new String(bytes);
                                String dataHex = Utils.byteToHexString(bytes);
                                String dataBase64 = Base64.encodeToString(bytes,0);
                                Log.d(log_d_tag,"-");
                                Log.d(log_d_tag,"-");
                                Log.d(log_d_tag,"=====================================HOOK开始==============================================");
                                Log.d(log_d_tag,"=====================================Class_MD5==============================================");
                                Log.d(log_d_tag,"--digest方法有参数参数");
                                String parameter_length = param.args.length+"";
                                Log.d(log_d_tag,"parameter_length:"+parameter_length);
                                Log.d(log_d_tag,algorithm+"_digest:"+data);
                                Log.d(log_d_tag,algorithm+"_digest_Hex:"+dataHex);
                                Log.d(log_d_tag,algorithm+"_digest_Base64:"+dataBase64);
                                Log.d(log_d_tag,"============================算法:"+algorithm+"方法:digest=================================");
                                Log.d(log_d_tag,"=====================================HOOK结束==============================================");
                            }
                        }

                        byte[] bytes2 = (byte[]) param.getResult();
                        String dataHex2 = Utils.byteToHexString(bytes2);
                        String dataBase642 = Base64.encodeToString(bytes2,0);
                        Log.d(log_d_tag,"-");
                        Log.d(log_d_tag,"-");
                        Log.d(log_d_tag,"=====================================HOOK开始==============================================");
                        Log.d(log_d_tag,"=====================================Class_MD5==============================================");
                        Log.d(log_d_tag,"--digest方法没参数");
                        Log.d(log_d_tag,algorithm+"_digestHex_Result:"+dataHex2);
                        Log.d(log_d_tag,algorithm+"_digestBase64_Result:"+dataBase642);
                        Log.d(log_d_tag,"=============================算法:"+algorithm+"方法:digest=================================");
                        Log.d(log_d_tag,"=====================================HOOK结束==============================================");
                        Log.d(log_d_tag,"=====================================调用堆栈~开始==============================================");
                        Log.d(log_d_tag,Log.getStackTraceString(new Throwable()));
                        Log.d(log_d_tag,"=====================================调用堆栈~结束==============================================");
                    }
                });

    }
}
